#!/usr/bin/env bash

# your anaconda env !![maybe need to change]!!
# if you don't have any conda env, maybe need to comment this line
conda activate dlpro

# pipeline.config path  !![must need to change]!!
PIPELINE_CONFIG_PATH=/Your_path_to/pipeline.config

# model.ckpt save path  !![must need to change]!!
TRAIN_DIR=/Your_path_of_train_dir/

# frozen_inference_graph.pb save path  !![must need to change]!!
FROZEN_MODEL_OUTPUT_DIR=/Your_save_path_of_forzen_inference_graph.pb/

# label.pbtxt path  !![must need to change]!!
LABEL_MAP_PATH=/Your_path_to/label_map.pdtxt

# test image dir  !![must need to change]!!
TEST_IMAGE_DIR=/Your_path_to_test_images/

# pid.txt file path, save the pid of training process  !![must need to change]!!
PID_FILE_PATH=/Your_path_of/pid.txt

while true
do
	nohup python /home/aimlab/linhongli/models/research/object_detection/train.py \
	--train_dir ${TRAIN_DIR} \
	--pipeline_config_path ${PIPELINE_CONFIG_PATH} &
	pid=$!
	echo ${pid} > ${PID_FILE_PATH}
	
	python /home/aimlab/linhongli/shanghai/test/auto_export_and_train.py \
	--train_dir ${TRAIN_DIR} \
	--pipeline_config_path ${PIPELINE_CONFIG_PATH} \
	--output_dir ${FROZEN_MODEL_OUTPUT_DIR} \
	--label_map_path ${LABEL_MAP_PATH} \
	--image_dir ${TEST_IMAGE_DIR} \
	--pid_file_path ${PID_FILE_PATH} \
	--min_iter 100000 \
	--iter_gap 100 
	
	# min_iter: Minimum iterator numbers of first test model.
	# iter_gap: Minimum iterator numbers between test models.

	return_value=$?
	if [[ ${return_value} != 0 ]]; then
		echo "Abnormal exit with return value $return_value"
		kill -9 ${pid}
		exit
	fi
done

echo "Finish!"
